package com.summerli.controller;

import com.summerli.common.ResponseResult;
import com.summerli.common.seriviceverificationcode.ShortMsgRequest;
import com.summerli.service.ShortMsgService;
import com.summerli.service.VerificationCodeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 短信业务服务
 */
@RestController
@RequestMapping("/sms")
@Slf4j
public class SmsController {

    @Autowired
    private VerificationCodeService verificationCodeService;

    @Autowired
    private ShortMsgService shortMsgService;

    /**
     * 获取验证码并且发送验证码
     *
     * @param request
     * @return
     */
    @PostMapping("/verify-code/send")
    public ResponseResult verifyCodeSend(@RequestBody @Validated ShortMsgRequest request){
        String code = verificationCodeService.getCode(request.getPhoneNumber());
        if(StringUtils.isNotBlank(code)){
            // 发送验证码
            return shortMsgService.send(request.getPhoneNumber(),code);
        }else{
            return ResponseResult.fail(2, "验证码发送次数过多，请您稍后再试");
        }
    }
}
